﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ArivisTests.Models;

namespace ArivisTests.Controllers
{
    [Authorize]
    public class TestCaseController : Controller
    {
        private ArivisTestsDBContext db = new ArivisTestsDBContext();

        //
        // GET: /TestCase/

        public ActionResult Index()
        {
            ViewBag.db = db;
            return View(db.TestCases.ToList());
        }

        //
        // GET: /TestCase/Details/5

        public ActionResult Details(int id = 0)
        {
            TestCase testcase = db.TestCases.Find(id);
            if (testcase == null)
            {
                return HttpNotFound();
            }
            ViewBag.db = db;
            return View(testcase);
        }

        //
        // GET: /TestCase/Create

        public ActionResult Create()
        {
            ViewBag.db = db;
            return View();
        }

        //
        // POST: /TestCase/Create

        [HttpPost]
        public ActionResult Create(TestCase testcase)
        {
            if (ModelState.IsValid)
            {
                db.TestCases.Add(testcase);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(testcase);
        }

        //
        // GET: /TestCase/Edit/5

        public ActionResult Edit(int id = 0)
        {
            TestCase testcase = db.TestCases.Find(id);
            ViewBag.Results = db.TestResults;
            ViewBag.Users = db.ActiveTester;
            ViewBag.UrlReferrer = this.Request.UrlReferrer;

            if (testcase == null)
            {
                return HttpNotFound();
            }
            return View(testcase);
        }

        //
        // POST: /TestCase/Edit/5

        [HttpPost]
        public ActionResult Edit(TestCase testcase, int? selectedResult, int? selectedUser, string UrlReferrer)
        {
            // get testcase
            var dbTestCase = db.TestCases.First(x => x.Id == testcase.Id);
            var dbResult = db.TestResults.FirstOrDefault(x => x.Id == selectedResult);
            var dbUser = db.UserProfiles.FirstOrDefault(x => x.UserId == selectedUser);

            // update testcase
            dbTestCase.Comment = testcase.Comment;
            dbTestCase.Tickets = testcase.Tickets;
            if (dbTestCase.Result != dbResult) { // this forces dbTestCase to load the result
                dbTestCase.Result = dbResult;
            }
            if (dbTestCase.User != dbUser) { // this forces dbTestCase to load the user
                dbTestCase.User = dbUser;
            }
            db.SaveChanges();
            return Redirect(UrlReferrer);
        }

        //
        // GET: /TestCase/Delete/5

        public ActionResult Delete(int id = 0)
        {
            TestCase testcase = db.TestCases.Find(id);
            if (testcase == null)
            {
                return HttpNotFound();
            }
            return View(testcase);
        }

        //
        // POST: /TestCase/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            // find test case and maybe test run
            TestCase testcase = db.TestCases.Find(id);
            int? testRunId = (testcase.Run == null) ? null : new int?(testcase.Run.Id);
            db.TestCases.Remove(testcase);
            db.SaveChanges();

            if (testRunId.HasValue)
                return RedirectToAction("Details", "TestRun", new { id = testRunId.Value });
            else
                return RedirectToAction("Index", "TestRun");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}